
import java.lang.reflect.Method;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Peter
 */
public class MethodProxy implements RefactoringProxy
{

    String className;
    Vector<String> returnValue = new Vector<String>();
    String descriptionString = "";
    
    /**
     * The default constructor that initializes the className as empty.
     */
    MethodProxy()
    {
        className = "";
    }
    
    /**
     * The constructor that initializes the className to the name given.
     * @param name String. The name of the class to be used.
     */
    MethodProxy(String name)
    {
        className = name;
    }
    
    @Override
    public Vector<String> refactor(Vector<String> parameter)
    {
        try 
        {
            PetersClassLoader myLoader = new PetersClassLoader();

            Class myClass = myLoader.loadClass(className);

            RefactoringMethod whatInstance = (RefactoringMethod) myClass.newInstance();
            //String methodParameter = "a quick brown fox";

            Method myMethod = myClass.getMethod("refactor", new Class[] { Vector.class });
            //Method myMethod = myClass.getMethod("refactor", new Class[]{}); 

            returnValue = (Vector<String>) myMethod.invoke(whatInstance, new Object[] { parameter });
            //Object returnValue = myMethod.invoke(whatInstance, null);
            //System.out.println(returnValue);
        } 
        catch (Exception ex) 
        {
            Logger.getLogger(MethodProxy.class.getName()).log(Level.SEVERE, null, ex);
        } 
        
        return returnValue;
    }

    @Override
    public String description() 
    {
        try 
        {
            PetersClassLoader myLoader = new PetersClassLoader();

            Class myClass = myLoader.loadClass(className);

            RefactoringMethod whatInstance = (RefactoringMethod) myClass.newInstance();
            //String methodParameter = "a quick brown fox";

            Method myMethod = myClass.getMethod("description");
            //Method myMethod = myClass.getMethod("refactor", new Class[]{}); 

            descriptionString = (String) myMethod.invoke(whatInstance);
            //Object returnValue = myMethod.invoke(whatInstance, null);
            //System.out.println(returnValue);
        } 
        catch (Exception ex) 
        {
            Logger.getLogger(MethodProxy.class.getName()).log(Level.SEVERE, null, ex);
        } 
        
        return descriptionString;
    }
    
}
